package cn.jinjimi.app.main.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import cn.jinjimi.app.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Jinsen 2015/6/24.
 */
public class CustomAddPhoneTypeDialog extends Dialog {

    SelectPhoneTypeListener mListener = null;

    private int mCurrentTyepIndex = 0;

    static List<String> mTypeNameList = new ArrayList<String>() {
        {
            add("手机");
            add("住宅");
            add("工作");
            add("主要");
            add("传真");
            add("其他");
        }
    };

    List<Integer> mSelectType = new ArrayList<Integer>();

    private TypeListAdapter mAdapter = new TypeListAdapter();

    private Context mContext = null;

    public CustomAddPhoneTypeDialog(Context context) {
        super(context);
        mContext = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_custom_add_phone_type_list);
        initView();
    }

    protected void initView() {
        ListView mTypeList = (ListView) findViewById(R.id.type_listView);
        mTypeList.setAdapter(mAdapter);
        mTypeList.setSelector(R.color.transparent);

        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setWindowAnimations(R.style.pop_bottom_in_out_anim_style);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(-1, -2);
        setCanceledOnTouchOutside(true);
    }

    public void setListener(SelectPhoneTypeListener listener) {
        mListener = listener;
    }

    public static String getTypeNameById(int index) {
        if (index < 0 || index > mTypeNameList.size() - 1) {
            index = mTypeNameList.size() - 1;
        }

        return mTypeNameList.get(index);
    }

    public void removeType(int index) {
        mSelectType.remove(Integer.valueOf(index));
    }

    public void show(int curTypeId) {
        super.show();
        mCurrentTyepIndex = curTypeId;
        mAdapter.notifyDataSetChanged();
    }

    public class TypeListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mTypeNameList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.custom_add_phone_type_item, null);
                holder.typeName = (TextView) convertView.findViewById(R.id.item_phone_type_name_textView);
                holder.hasSelectMark = (ImageView) convertView.findViewById(R.id.item_has_select_imageView);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.typeName.setText(mTypeNameList.get(position));

            if (mCurrentTyepIndex == position) {
                holder.hasSelectMark.setVisibility(View.VISIBLE);
                convertView.setOnClickListener(null);
            } else {
                holder.hasSelectMark.setVisibility(View.GONE);
                convertView.setOnClickListener(new SelectTypeListener(position));
            }

            return convertView;
        }

        class Holder {
            public TextView typeName;
            public ImageView hasSelectMark;
        }
    }

    class SelectTypeListener implements View.OnClickListener {

        private int mIndex = -1;

        public SelectTypeListener(int id) {
            mIndex = id;
        }

        @Override
        public void onClick(View v) {
            mCurrentTyepIndex = mIndex;
            if (mListener != null) {
                mListener.selectType(mIndex, getTypeNameById(mIndex));
            }
            dismiss();
        }
    }

    public interface SelectPhoneTypeListener {
        void selectType(int index, String name);
    }
}
